package com.wyl.pmd.rule.performance;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class AvoidFileStream {

	public static void main(String[] args) {
		
		String fileName = "E:\\logs\\udb.log";
		
		//bad
		try {
			FileInputStream fis = new FileInputStream(fileName);
		    FileOutputStream fos = new FileOutputStream(fileName);
			FileReader fr = new FileReader(fileName);
			FileWriter fw = new FileWriter(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    

	    //good
	    try(InputStream is = Files.newInputStream(Paths.get(fileName))) {
	    	
	    } catch (IOException e) {
			e.printStackTrace();
		}
	    try(OutputStream os = Files.newOutputStream(Paths.get(fileName))) {
	    	
	    } catch (IOException e) {
			e.printStackTrace();
		}
	    try(BufferedReader br = Files.newBufferedReader(Paths.get(fileName), StandardCharsets.UTF_8)) {
	    	
	    } catch (IOException e) {
			e.printStackTrace();
		}
	    try(BufferedWriter wr = Files.newBufferedWriter(Paths.get(fileName), StandardCharsets.UTF_8)) {
	    	
	    } catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
}
